﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Forums.ForumsClient.Controllers;
using Microsoft.Forums.ForumsClient.Models;
using Microsoft.Forums.OfflineStore;
using System.Collections.ObjectModel;

namespace Microsoft.Forums.ForumsClient.Views
{
    /// <summary>
    /// Interaction logic for SubscribeToForumsView.xaml
    /// </summary>
    public partial class SubscribeToForumsView : UserControl
    {
        private SubscribeToForumsDataModel subscribeDataModel;
        private SubscribeToForumsController controller;

        public SubscribeToForumsView()
        {
            InitializeComponent();
            this.DataContextChanged += View_DataContextChanged;

        }



        #region AddToPreviewArea Command Handlers
        private void AddToPreviewArea_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            // The controller will be null before the DataContext is initialized.
            // If it is null, we don't want to try to add to the collection bound
            // to the PreviewForumsListView control.
            e.CanExecute = (controller != null ) && (controller.CanAddToPreviewArea);
        }

        /// <summary>
        /// Makes a collection of the selected forums and 
        /// then uses the local collection to add them to the preview area.
        /// The local collection is necessary because they are also removed
        /// from the collection bound to AvailableForumsListView control and 
        /// you cannot iterate over a collection that is changing. 
        /// That would throw an exception!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddToPreviewArea_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ObservableCollection<Forum> selectedForums = new ObservableCollection<Forum>();
            foreach (Forum forum in this.AvailableForumsListView.SelectedItems)
            {
                selectedForums.Add(forum);
            }

            foreach (Forum forum in selectedForums)
            {
                controller.AddToPreviewArea(forum);
            }
        }
        #endregion

        #region RemoveFromPreviewArea Command Handlers

        private void RemoveFromPreviewArea_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            // The controller will be null before the DataContext is initialized.
            // If it is null, we don't want to try to remove things from the collection bound
            // to the PreviewForumsListView control.
            e.CanExecute = (controller != null) && (controller.CanRemoveFromPreviewArea);
        }

        /// <summary>
        /// Makes a collection of the selected forums and then uses the 
        /// local collection to remove them from the collection bound to the
        /// PreviewForumsListView control. Using the local collection prevents
        /// exceptions due to the bound collection changing while we're iterating.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RemoveFromPreviewArea_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            ObservableCollection<Forum> selectedForums = new ObservableCollection<Forum>();
            foreach (Forum forum in this.PreviewForumsListView.SelectedItems)
            {
                selectedForums.Add(forum);
            }

            foreach (Forum forum in selectedForums)
            {
                controller.RemoveFromPreviewArea(forum);
            }
        }
        #endregion

        /// <summary>
        /// When the DataContext is finally initialized or changes,
        /// this method will create a new instance of the controller.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void View_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            subscribeDataModel = (SubscribeToForumsDataModel)this.DataContext;
            controller = new SubscribeToForumsController(subscribeDataModel);
        }

    }
}
